<?php

/*
 * To change this template, choose Tools | Templates
* and open the template in the editor.
*/

class HT_Model_administrator_models_district extends Zend_Db_Table {//ten class fai viet hoa

	protected $_db;

	public function __construct() {
		$this->_name = "district";
		$this->_db = Zend_Registry::get('dbMain');
		parent::init();
	}
	
	public function addDistrict($data){
		$provinceId 	= (int)$data['province_id'];
		$districtName 	= trim($data['district_name']);
		if(!$this->_checkExistsDistrict($provinceId,$districtName)){
			$this->insert($data);
			return $this->getMaxId();
		}else{
			return "-1";
		}
	}
	
	public function updateDistrict($data,$districtId){
		$provinceId 	= (int)$data['province_id'];
		$districtName 	= trim($data['district_name']);
		if(!$this->_checkExistsDistrict($provinceId,$districtName,'update',$districtId)){
			$this->update($data, "district_id=".(int)$districtId);
			return $districtId;
		}else{
			return "-1";
		}
	}
	
	private function _checkExistsDistrict($provinceId,$districtName,$type='insert',$districtId = null){
		if($type == 'insert'){
			$sql = "SELECT COUNT(district_id) FROM district WHERE province_id = ? AND district_name = ?";
			return  (int)$this->_db->fetchOne($sql,array($provinceId,$districtName));
		}else{
			$sql = "SELECT COUNT(district_id) FROM district WHERE province_id = ? AND district_name = ? AND district_id <> ?";
			return  (int)$this->_db->fetchOne($sql,array($provinceId,$districtName,$districtId));
		}
	}
	
	public function getMaxId(){
		$sql = "SELECT MAX(district_id) FROM district";
		return  (int)$this->_db->fetchOne($sql);
	}
	public function getDistrict($districtId,$filter = array()) {
		$sql = "SELECT dt.district_id, dt.district_name,dt.district_code, pv.province_id, pv.province_name
				FROM district dt
				INNER JOIN province pv ON dt.province_id = pv.province_id
				WHERE dt.district_id= ".(int)$districtId;
		return $this->_db->fetchRow($sql);
	}
	public function getListDistrict_nb($filter = array()) {
		$sqlPlus = $this->getListDistrict_sqlPlus($filter);
		$sql = "SELECT COUNT(dt.district_id)
				FROM district dt
				INNER JOIN province pv ON dt.province_id = pv.province_id
				WHERE 1=1 $sqlPlus";
		return $this->_db->fetchOne($sql);
	}
	public function getListDistrict($start=0,$size = 10,$filter = array()) {
		$sqlPlus = $this->getListDistrict_sqlPlus($filter);
		$sql = "SELECT dt.district_id, dt.district_name,dt.district_code, pv.province_id, pv.province_name
				FROM district dt
				INNER JOIN province pv ON dt.province_id = pv.province_id
				WHERE 1=1 $sqlPlus ORDER BY dt.district_name LIMIT $start,$size";
		return $this->_db->fetchAll($sql);
	}
	
	private function getListDistrict_sqlPlus($filter){
		$sqlPlus = null;
		foreach($filter as $key => $val){
			$key = trim($key);
			$val = trim($val);
			switch($key){
				case 'keyword':
					if($val) $sqlPlus .= " AND (dt.district_name LIKE '%$val%' OR pv.province_name LIKE '%$val%') ";
					break;
				case 'province_id':
					if((int)$val >0) $sqlPlus .= " AND pv.province_id = ".(int)$val;
					break;
			}
		}
		return $sqlPlus;
	}
	
}

?>
